﻿<?php
include_once("../initial.php");
include_once("../dbdriver/userWHandle.php");
include_once("../dbdriver/userRHandle.php");
class userLogic
{
    public function insert_user($array)
    {
        if(is_array($array)==false)
        {
            return false;
        }
        if(count($array)==0)
        {
            return false;
        }
        $whandle = new userWHandle();
        return $whandle->insert_data($array);
    }
    public function checkUser($uid='', $password='')
    {
    		$rhandle = new userRHandle();
    		if($uid=='' || $password=='')
    		{
    				return false;
    		}
    		$array = array('id'=>$uid, "passwd"=>md5($password));
    		$number = $rhandle->get_user_num($array);
    		if($number<1)
    		{
    				return false;
    		}
    		return true;
    }
    public function get_user($array='', $orderby='', $limit='')
    {
        $rhandle = new userRHandle();
        if($array == '')
        {
            if($orderby=='')
            {
                $orderby="order by createtime desc";
            }
            return $rhandle->get_all_data($orderby,$limit);
        }
        else
        {
            return $rhandle->get_some_data($array, $orderby, $limit);
        }
    }
    public function get_user_num($array='')
    {
        $rhandle = new userRHandle();
        return $rhandle->get_user_num($array);
    }
    public function update_user($array,$id)
    {
        $handle = new userWHandle();
        return $handle->update_user($array,$id);
    }
    public function delete_user($id)
    {
        $handle = new userWHandle();
        return $handle->delete_user($id);
    }
    public function delete_user_phy($id)
    {
        $handle = new userWHandle();
        return $handle->delete_user_phy($id);
    }
    public function get_course_number_by_user($uid)
    {
    		$rh = new userRHandle();
    		$array=array("uid"=>$uid);
    		return $rh->get_user_course_number($array);
    }
    public function get_user_course_number($uid, $cid, $booktime='', $createtime='')
    {
    		$rh = new userRHandle();
    		$array=array("uid"=>$uid, "cid"=>$cid);
    		if($booktime!='')
    		{
    				$array["booktime"]=$booktime;
    			}
    		if($createtime!='')
    		{
    				$array["createtime"]=$createtime;
    			}
    		return $rh->get_user_course_number($array);
    }
    public function insert_user_course($uid, $cid, $booktime='', $createtime='')
    {
    		if($uid=='' || $cid=='')
        {
        		return false;
        }
        $handle = new userWHandle();
        if($createtime=='')
        {
        		$createtime=date("Y-m-d H:i:s");
        }
        $array = array("uid"=>"$uid", "cid"=>"$cid", "booktime"=>"$booktime", "createtime"=>"$createtime");
        return $handle->insert_user_course($array);
    }
    
    //please modify these functions below
    public function update_user_course_booktime($userId, $courseId, $booktime)
    {
        $handle = new userWHandle();
        return $handle->update_user_course_booktime($userId, $courseId, $booktime);
    }
    public function delete_user_course($uid, $cid)
    {
        $handle = new userWHandle();
        $condition=array("uid"=>$uid, "cid"=>$cid);
        return $handle->delete_user_course($condition);
    }
    public function get_user_courses($uid, $orderby='order by createtime desc', $limit='')
    {
        $rhandle = new userRHandle();
        $array=array("uid"=>$uid);
        return $rhandle->get_user_courses($array, $orderby, $limit);
    }
    //只显示部分结果
    public function search_user_by_keyword($keyword='', $limit='')
    {
    		if(strlen($keyword)<3)
    		{
    				return array();
    		}
    		$rhandle = new userRHandle();
    		$result = $rhandle->search_user_by_keyword($keyword, $limit);
    		return $result;
    }
    public function reset_user_password($uid)
    {
    		$passwd=md5($uid);
    		$uw = new userWHandle();
    		return $uw->update_user(array("passwd"=>$passwd), $uid);
    }
}
?>
